/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.persistence;

import org.hibernate.Session;
import org.hibernate.Transaction;
import systole.persistence.brokersDB.AnalysisBrokerDB;
import systole.persistence.brokersDB.AnalysisResultBrokerDB;
import systole.persistence.brokersDB.AnalysisSyncBrokerDB;
import systole.persistence.brokersDB.AveragesBrokerDB;
import systole.persistence.brokersDB.ClinicalInfoBrokerDB;
import systole.persistence.brokersDB.ClinicalInfoSyncBrokerDB;
import systole.persistence.brokersDB.CommentBrokerDB;
import systole.persistence.brokersDB.ComparativeBrokerDB;
import systole.persistence.brokersDB.ConclusionBrokerDB;
import systole.persistence.brokersDB.FamilyBrokerDB;
import systole.persistence.brokersDB.PathologyBrokerDB;
import systole.persistence.brokersDB.SportBrokerDB;
import systole.persistence.brokersDB.SurgeryBrokerDB;
import systole.persistence.brokersDB.FamilyBackgroundBrokerDB;
import systole.persistence.brokersDB.FamilySyncBrokerDB;
import systole.persistence.brokersDB.IdentityCardBrokerDB;
import systole.persistence.brokersDB.IdentityCardTypeSyncBrokerDB;
import systole.persistence.brokersDB.MedicBrokerDB;
import systole.persistence.brokersDB.MedicSyncBrokerDB;
import systole.persistence.brokersDB.MedicineBrokerDB;
import systole.persistence.brokersDB.MedicineByPatientBrokerDB;
import systole.persistence.brokersDB.MedicineSyncBrokerDB;
import systole.persistence.brokersDB.PathologyByPatienBrokerDB;
import systole.persistence.brokersDB.PathologySyncBrokerDB;
import systole.persistence.brokersDB.PatientBrokerDB;
import systole.persistence.brokersDB.PatientSyncBrokerDB;
import systole.persistence.brokersDB.ProfessionBrokerDB;
import systole.persistence.brokersDB.ProfessionSyncBrokerDB;
import systole.persistence.brokersDB.RawSignalBrokerDB;
import systole.persistence.brokersDB.ReportBrokerDB;
import systole.persistence.brokersDB.SignalFrequencyBrokerDB;
import systole.persistence.brokersDB.SignalFrequencySyncBrokerDB;
import systole.persistence.brokersDB.SportByPatientBrokerDB;
import systole.persistence.brokersDB.SportSyncBrokerDB;
import systole.persistence.brokersDB.StandardValuesBrokerDB;
import systole.persistence.brokersDB.SurgeryByPatientBrokerDB;
import systole.persistence.brokersDB.SurgerySyncBrokerDB;
import systole.persistence.brokersInterface.AnalysisBroker;
import systole.persistence.brokersInterface.AnalysisResultBroker;
import systole.persistence.brokersInterface.AnalysisSyncBroker;
import systole.persistence.brokersInterface.AveragesBroker;
import systole.persistence.brokersInterface.ClinicalInfoBroker;
import systole.persistence.brokersInterface.ClinicalInfoSyncBroker;
import systole.persistence.brokersInterface.CommentBroker;
import systole.persistence.brokersInterface.ComparativeBroker;
import systole.persistence.brokersInterface.ConclusionBroker;
import systole.persistence.brokersInterface.FamilyBackgroundBroker;
import systole.persistence.brokersInterface.FamilyBroker;
import systole.persistence.brokersInterface.FamilySyncBroker;
import systole.persistence.brokersInterface.IdentityCardBroker;
import systole.persistence.brokersInterface.IdentityCardTypeSyncBroker;
import systole.persistence.brokersInterface.MedicBroker;
import systole.persistence.brokersInterface.MedicSyncBroker;
import systole.persistence.brokersInterface.MedicineBroker;
import systole.persistence.brokersInterface.MedicineByPatientBroker;
import systole.persistence.brokersInterface.MedicineSyncBroker;
import systole.persistence.brokersInterface.PathologyBroker;
import systole.persistence.brokersInterface.PathologyByPatientBroker;
import systole.persistence.brokersInterface.PathologySyncBroker;
import systole.persistence.brokersInterface.PatientBroker;
import systole.persistence.brokersInterface.PatientSyncBroker;
import systole.persistence.brokersInterface.ProfessionBroker;
import systole.persistence.brokersInterface.ProfessionSyncBroker;
import systole.persistence.brokersInterface.RawSignalBroker;
import systole.persistence.brokersInterface.ReportBroker;
import systole.persistence.brokersInterface.SignalFrequencyBroker;
import systole.persistence.brokersInterface.SignalFrequencySyncBroker;
import systole.persistence.brokersInterface.SportBroker;
import systole.persistence.brokersInterface.SportByPatientBroker;
import systole.persistence.brokersInterface.SportSyncBroker;
import systole.persistence.brokersInterface.StandardValuesBroker;
import systole.persistence.brokersInterface.SurgeryBroker;
import systole.persistence.brokersInterface.SurgeryByPatientBroker;
import systole.persistence.brokersInterface.SurgerySyncBroker;

/**
 * 
 * @author jmj
 */
public class FacadeDB {

    private Session session;
    private Transaction currentTransaction;
    // general brokers
    private AnalysisBroker analysisBroker = null;
    private AnalysisResultBroker analysisResultBroker = null;
    private MedicBroker medicBroker = null;
    private PatientBroker patientBroker = null;
    private RawSignalBroker rawSignalBroker = null;
    private ReportBroker reportBroker = null;
    private SignalFrequencyBroker signalFrequencyBroker = null;
    private IdentityCardBroker identityCardBroker = null;
    private MedicineBroker medicineBroker = null;
    private PathologyBroker pathologyBroker = null;
    private SurgeryBroker surgeryBroker = null;
    private SportBroker sportBroker = null;
    private ConclusionBroker conclusionBroker = null;
    private CommentBroker commentBroker = null;
    private ComparativeBroker comparativeBroker = null;
    private AveragesBroker averagesBroker = null;
    private FamilyBroker familyBroker = null;
    private FamilyBackgroundBroker familyBackgroundBroker = null;
    private MedicineByPatientBroker medicineByPatientBroker = null;
    private PathologyByPatientBroker pathologyByPatienBroker = null;
    private SportByPatientBroker sportByPatientBroker = null;
    private SurgeryByPatientBroker surgeryByPatientBroker = null;
    private ClinicalInfoBroker clinicalInfoBroker = null;
    private ProfessionBroker professionBroker = null;
    private StandardValuesBroker standardValuesBroker = null;
    //sync brokers
    private PatientSyncBroker patientSyncBroker = null;
    private MedicSyncBroker medicSyncBroker = null;
    private MedicineSyncBroker medicineSyncBroker = null;
    private PathologySyncBroker pathologySyncBroker = null;
    private SportSyncBroker sportSyncBroker = null;
    private SurgerySyncBroker surgerySyncBroker = null;
    private SignalFrequencySyncBroker signalFrequencySyncBroker = null;
    private FamilySyncBroker familySyncBroker = null;
    private IdentityCardTypeSyncBroker identityCardTypeSyncBroker = null;
    private ClinicalInfoSyncBroker clinicalInfoSyncBroker = null;
    private AnalysisSyncBroker analysisSyncBroker = null;
    private ProfessionSyncBroker professionSyncBroker = null;
    // facade instance
    private static FacadeDB instance = null;

    private FacadeDB() {
        this.session = SessionManager.getInstance().getNewSession();
    }

    /**
     * @return the unique instance of facade
     */
    public static FacadeDB getInstance() {

        if (instance == null) {
            instance = new FacadeDB();
        }
        return instance;
    }

    /**
     * @return the broker for Analysis
     */
    public AnalysisBroker getAnalysisBroker() {
        if (this.analysisBroker == null) {
            this.analysisBroker = new AnalysisBrokerDB();
        }
        return this.analysisBroker;
    }

    /**
     * @return the broker for Result analysis
     */
    public AnalysisResultBroker getAnalysisResultBroker() {
        if (this.analysisResultBroker == null) {
            this.analysisResultBroker = new AnalysisResultBrokerDB();
        }
        return this.analysisResultBroker;
    }

    /**
     * @return the broker for medics
     */
    public MedicBroker getMedicBroker() {
        if (this.medicBroker == null) {
            this.medicBroker = new MedicBrokerDB();
        }
        return this.medicBroker;
    }

    /**
     * @return the broker
     */
    public PatientBroker getPatientBroker() {
        if (this.patientBroker == null) {
            this.patientBroker = new PatientBrokerDB();
        }
        return this.patientBroker;
    }

    /**
     * @return broker for raw signal
     */
    public RawSignalBroker getRawSignalBroker() {
        if (this.rawSignalBroker == null) {
            this.rawSignalBroker = new RawSignalBrokerDB();
        }
        return this.rawSignalBroker;
    }

    /**
     * @return the broker for report
     */
    public ReportBroker getReportBroker() {
        if (this.reportBroker == null) {
            this.reportBroker = new ReportBrokerDB();
        }
        return this.reportBroker;
    }

    /**
     * @return broker for frequency signal
     */
    public SignalFrequencyBroker getSignalFrequencyBroker() {
        if (this.signalFrequencyBroker == null) {
            this.signalFrequencyBroker = new SignalFrequencyBrokerDB();
        }
        return this.signalFrequencyBroker;
    }

    /**
     * @return broker for identity cards
     */
    public IdentityCardBroker getIdentityCardBroker() {
        if (this.identityCardBroker == null) {
            this.identityCardBroker = new IdentityCardBrokerDB();
        }
        return this.identityCardBroker;
    }

    /**
     * @return the medicineBroker
     */
    public MedicineBroker getMedicineBroker() {
        if (this.medicineBroker == null) {
            this.medicineBroker = new MedicineBrokerDB();
        }
        return this.medicineBroker;
    }

    /**
     * @return the pathologyeBroker
     */
    public PathologyBroker getPathologyBroker() {
        if (this.pathologyBroker == null) {
            this.pathologyBroker = new PathologyBrokerDB();
        }
        return this.pathologyBroker;
    }

    /**
     * @return the surgeryBroker
     */
    public SurgeryBroker getSurgeryBroker() {
        if (this.surgeryBroker == null) {
            this.surgeryBroker = new SurgeryBrokerDB();
        }
        return this.surgeryBroker;
    }

    /**
     * @return the sportBroker
     */
    public SportBroker getSportBroker() {
        if (this.sportBroker == null) {
            this.sportBroker = new SportBrokerDB();
        }
        return this.sportBroker;
    }

    /**
     * @return the conclusionBroker
     */
    public ConclusionBroker getConclusionBroker() {
        if (this.conclusionBroker == null) {
            this.conclusionBroker = new ConclusionBrokerDB();
        }
        return conclusionBroker;
    }

    /**
     * @return the commentBroker
     */
    public CommentBroker getCommentBroker() {
        if (this.commentBroker == null) {
            this.commentBroker = new CommentBrokerDB();
        }
        return commentBroker;
    }

    /**
     * @return the commentBroker
     */
    public AveragesBroker getAveragesBroker() {
        if (this.averagesBroker == null) {
            this.averagesBroker = new AveragesBrokerDB();
        }
        return averagesBroker;
    }

    /**
     * @return the comparativeBroker
     */
    public ComparativeBroker getComparativeBroker() {
        if (this.comparativeBroker == null) {
            this.comparativeBroker = new ComparativeBrokerDB();
        }
        return comparativeBroker;
    }

    /**
     * @return the familyBroker
     */
    public FamilyBroker getFamilyBroker() {
        if (this.familyBroker == null) {
            this.familyBroker = new FamilyBrokerDB();
        }
        return familyBroker;
    }

    /**
     * @return the familyBackgroundBroker
     */
    public FamilyBackgroundBroker getFamilyBackgroundBroker() {
        if (this.familyBackgroundBroker == null) {
            this.familyBackgroundBroker = new FamilyBackgroundBrokerDB();
        }
        return familyBackgroundBroker;
    }

    /**
     * @return the medicineByPatientBroker
     */
    public MedicineByPatientBroker getMedicineByPatientBroker() {
        if (this.medicineByPatientBroker == null) {
            this.medicineByPatientBroker = new MedicineByPatientBrokerDB();
        }
        return medicineByPatientBroker;
    }

    /**
     * @return the pathologyByPatienBroker
     */
    public PathologyByPatientBroker getPathologyByPatienBroker() {
        if (this.pathologyByPatienBroker == null) {
            this.pathologyByPatienBroker = new PathologyByPatienBrokerDB();
        }
        return pathologyByPatienBroker;
    }

    /**
     * @return the sportByPatientBroker
     */
    public SportByPatientBroker getSportByPatientBroker() {
        if (this.sportByPatientBroker == null) {
            this.sportByPatientBroker = new SportByPatientBrokerDB();
        }
        return sportByPatientBroker;
    }

    /**
     * @return the surgeryByPatientBroker
     */
    public SurgeryByPatientBroker getSurgeryByPatientBroker() {
        if (this.surgeryByPatientBroker == null) {
            this.surgeryByPatientBroker = new SurgeryByPatientBrokerDB();
        }
        return surgeryByPatientBroker;
    }

    /**
     * @return the clinicalInfoBroker
     */
    public ClinicalInfoBroker getClinicalInfoBroker() {
        if (this.clinicalInfoBroker == null) {
            this.clinicalInfoBroker = new ClinicalInfoBrokerDB();
        }
        return clinicalInfoBroker;
    }

    /**
     * @return the professionBroker
     */
    public ProfessionBroker getProfessionBroker() {
        if (this.professionBroker == null) {
            this.professionBroker = new ProfessionBrokerDB();
        }
        return professionBroker;
    }

    /**
     * 
     * @return  the standardValuesBroker
     */
    public StandardValuesBroker getStandardValuesBroker() {
        if (this.standardValuesBroker == null) {
            this.standardValuesBroker = new StandardValuesBrokerDB();
        }
        return standardValuesBroker;
    }

    /**
     * @return the patientSyncBroker
     */
    public PatientSyncBroker getPatientSyncBroker() {
        if (this.patientSyncBroker == null) {
            this.patientSyncBroker = new PatientSyncBrokerDB();
        }
        return patientSyncBroker;
    }

    /**
     * @return the medicSyncBroker
     */
    public MedicSyncBroker getMedicSyncBroker() {
        if (this.medicSyncBroker == null) {
            this.medicSyncBroker = new MedicSyncBrokerDB();
        }
        return medicSyncBroker;
    }

    /**
     * @return the medicineSyncBroker
     */
    public MedicineSyncBroker getMedicineSyncBroker() {
        if (this.medicineSyncBroker == null) {
            this.medicineSyncBroker = new MedicineSyncBrokerDB();
        }
        return medicineSyncBroker;
    }

    /**
     * @return the pathologySyncBroker
     */
    public PathologySyncBroker getPathologySyncBroker() {
        if (this.pathologySyncBroker == null) {
            this.pathologySyncBroker = new PathologySyncBrokerDB();
        }
        return pathologySyncBroker;
    }

    /**
     * @return the sportSyncBroker
     */
    public SportSyncBroker getSportSyncBroker() {
        if (this.sportSyncBroker == null) {
            this.sportSyncBroker = new SportSyncBrokerDB();
        }
        return sportSyncBroker;
    }

    /**
     * @return the surgerySyncBroker
     */
    public SurgerySyncBroker getSurgerySyncBroker() {
        if (this.surgerySyncBroker == null) {
            this.surgerySyncBroker = new SurgerySyncBrokerDB();
        }
        return surgerySyncBroker;
    }

    /**
     * @return the signalFrequencySyncBroker
     */
    public SignalFrequencySyncBroker getSignalFrequencySyncBroker() {
        if (this.signalFrequencySyncBroker == null) {
            this.signalFrequencySyncBroker = new SignalFrequencySyncBrokerDB();
        }
        return signalFrequencySyncBroker;
    }

    /**
     * @return the familySyncBroker
     */
    public FamilySyncBroker getFamilySyncBroker() {
        if (this.familySyncBroker == null) {
            this.familySyncBroker = new FamilySyncBrokerDB();
        }
        return familySyncBroker;
    }

    /**
     * @return the identityCardTypeSyncBroker
     */
    public IdentityCardTypeSyncBroker getIdentityCardTypeSyncBroker() {
        if (this.identityCardTypeSyncBroker == null) {
            this.identityCardTypeSyncBroker = new IdentityCardTypeSyncBrokerDB();
        }
        return identityCardTypeSyncBroker;
    }

    /**
     * @return the clinicalInfoSyncBroker
     */
    public ClinicalInfoSyncBroker getClinicalInfoSyncBroker() {
        if (this.clinicalInfoSyncBroker == null) {
            this.clinicalInfoSyncBroker = new ClinicalInfoSyncBrokerDB();
        }
        return clinicalInfoSyncBroker;
    }

    /**
     * @return the analysisSyncBroker
     */
    public AnalysisSyncBroker getAnalysisSyncBroker() {
    if (this.analysisSyncBroker==null){
        this.analysisSyncBroker = new AnalysisSyncBrokerDB();
    }
        return analysisSyncBroker;
    }

     /**
     * @return the professionSyncBroker
     */
    public ProfessionSyncBroker getProfessionSyncBroker() {
    if (this.professionSyncBroker==null){
        this.professionSyncBroker = new ProfessionSyncBrokerDB();
    }
        return professionSyncBroker;
    }

    /**
     * Start a transaction
     */
    public void startTransaction() {
        this.currentTransaction = this.session.beginTransaction();
    }

    /**
     * Commit current transaction
     */
    public void commitTransaction() {
        this.currentTransaction.commit();
        this.refreshSession();
    }

    /**
     * Roll back current transaction
     */
    public void rollbackTransaction() {
        this.currentTransaction.rollback();
        this.refreshSession();
    }

    /**
     * refresh current session
     */
    public void refreshSession() {
        if ((this.session != null) || (!this.session.isOpen())) {
            this.session.clear();
            this.session.close();
        }
        this.session = SessionManager.getInstance().getNewSession();
    }

    /**
     * @return the current session
     */
    public Session getCurrentSession() {
        return this.session;
    }
}
